/*     */ package com.wzh.filter;
/*     */ 
/*     */ import com.opensymphony.oscache.base.Cache;
/*     */ import com.opensymphony.oscache.base.NeedsRefreshException;
/*     */ import com.opensymphony.oscache.web.ServletCacheAdministrator;
/*     */ import com.opensymphony.oscache.web.filter.CacheHttpServletResponseWrapper;
/*     */ import com.opensymphony.oscache.web.filter.ResponseContent;
/*     */ import com.wzh.utils.PropertiesUtil;
/*     */ import java.io.IOException;
/*     */ import javax.servlet.Filter;
/*     */ import javax.servlet.FilterChain;
/*     */ import javax.servlet.FilterConfig;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.ServletRequest;
/*     */ import javax.servlet.ServletResponse;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ 
/*     */ public class CacheFilter
/*     */   implements Filter
/*     */ {
/*  28 */   private final Log log = LogFactory.getLog(getClass());
/*     */   private FilterConfig config;
/*  32 */   private ServletCacheAdministrator admin = null;
/*  33 */   private int cacheScope = 4;
/*     */   private int time;
/*     */ 
/*     */   public void destroy()
/*     */   {
/*     */   }
/*     */ 
/*     */   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
/*     */     throws ServletException, IOException
/*     */   {
/*  55 */     this.log.info("<cache>: filter in scope " + this.cacheScope);
/*     */ 
/*  57 */     HttpServletRequest httpRequest = (HttpServletRequest)request;
/*  58 */     String key = this.admin.generateEntryKey(null, httpRequest, this.cacheScope);
/*  59 */     Cache cache = this.admin.getCache(httpRequest, this.cacheScope);
/*     */     try
/*     */     {
/*  62 */       ResponseContent respContent = (ResponseContent)cache.getFromCache(key, this.time);
/*  63 */       this.log.info("<cache>: Using cached entry for " + key);
/*     */ 
/*  65 */       respContent.writeTo(response);
/*     */     } catch (NeedsRefreshException nre) {
/*  67 */       boolean updateSucceeded = false;
/*     */       try
/*     */       {
/*  70 */         this.log.info("<cache>: New cache entry, cache stale or cache scope flushed for " + key);
/*     */ 
/*  73 */         CacheHttpServletResponseWrapper cacheResponse = new CacheHttpServletResponseWrapper((HttpServletResponse)response);
/*  74 */         chain.doFilter(request, cacheResponse);
/*  75 */         cacheResponse.flushBuffer();
/*     */ 
/*  78 */         if (cacheResponse.getStatus() == 200)
/*     */         {
/*  80 */           cache.putInCache(key, cacheResponse.getContent());
/*  81 */           updateSucceeded = true;
/*     */         }
/*     */       } finally {
/*  84 */         if (!updateSucceeded)
/*  85 */           cache.cancelUpdate(key);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public void init(FilterConfig filterConfig)
/*     */   {
/*  96 */     PropertiesUtil properUtil = new PropertiesUtil("cache.properties");
/*  97 */     this.time = Integer.parseInt(properUtil.getProperty("cache.time"));
/*     */ 
/*  99 */     this.config = filterConfig;
/* 100 */     this.admin = ServletCacheAdministrator.getInstance(this.config.getServletContext());
/*     */     try
/*     */     {
/* 104 */       this.time = Integer.parseInt(this.config.getInitParameter("time"));
/*     */     } catch (Exception e) {
/* 106 */       this.log.info("Could not get init paramter 'time', defaulting to one hour.");
/*     */     }
/*     */     try
/*     */     {
/* 110 */       String scopeString = this.config.getInitParameter("scope");
/*     */ 
/* 112 */       if (scopeString.equals("session"))
/* 113 */         this.cacheScope = 3;
/* 114 */       else if (scopeString.equals("application"))
/* 115 */         this.cacheScope = 4;
/* 116 */       else if (scopeString.equals("request"))
/* 117 */         this.cacheScope = 2;
/* 118 */       else if (scopeString.equals("page"))
/* 119 */         this.cacheScope = 1;
/*     */     }
/*     */     catch (Exception e) {
/* 122 */       this.log.info("Could not get init paramter 'scope', defaulting to 'application'");
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.filter.CacheFilter
 * JD-Core Version:    0.6.1
 */